<template>
  <div ref="bottom">
    <!-- 没有更多 -->
    <div v-if="finish"
         class="none">
      <img :src="noMoreImg"/>
      <p>亲，没有更多了</p>
    </div>
    <!-- 加载动画 -->
    <div v-else-if="loading"
         class="loading">
      <loading/>
    </div>
  </div>
</template>

<script lang="ts"
        setup>
import noMoreImg from '@/assets/images/no-more.png'
import Loading from '@/components/library/Loading.vue'
import {onBeforeUnmount, onMounted, ref} from 'vue'

let props = defineProps<{
  loadMore: Function
}>()

let finish = ref<boolean>(false)
let loading = ref<boolean>(false)
let bottom = ref<HTMLElement>()
let ob: IntersectionObserver

onMounted(() => {
  ob = new IntersectionObserver(async ([{isIntersecting}]) => {
    if (isIntersecting && !loading.value) {
      loading.value = true
      finish.value = !await props.loadMore()
      loading.value = false
    }
  })
  ob.observe(<HTMLElement>bottom.value)
})

onBeforeUnmount(() => {
  ob.disconnect()
})
</script>

<style lang="less"
       scoped>
.none {
  display: flex;
  align-items: center;
  justify-content: center;

  img {
    width: 200px;
    height: 134px;
    object-fit: cover;
  }

  p {
    color: #999;
    font-size: 16px;
  }
}

.loading {
  height: 150px;
}
</style>